/*
+--------------------------------------------------------------------------
|   Mblog [#RELEASE_VERSION#]
|   ========================================
|   Copyright (c) 2014, 2019 mtons. All Rights Reserved
|   http://www.mtons.com
|
+---------------------------------------------------------------------------
*/
package com.example.template.common.storage.impl;


import com.example.template.common.api.ApiCodeConstant;
import com.example.template.common.config.SiteOptions;
import com.example.template.common.exception.GlobalException;
import com.example.template.common.storage.FileOperation;
import com.example.template.utils.FileKit;
import com.example.template.utils.FilePathUtils;
import com.example.template.utils.ImageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * 抽象的基础存储方案 实现存储动作接口
 */
public abstract class AbstractStorage implements FileOperation {

    @Autowired
    protected SiteOptions options;

    //验证文件是否为空以及格式是否正常
    protected void validateFile(MultipartFile file) {
        if (file == null || file.isEmpty()) {
            throw new GlobalException(ApiCodeConstant.RESULT_OTHER_EXCEPTION,"文件不能为空");
        }
        if (!FileKit.checkFileType(file.getOriginalFilename())) {
            throw new GlobalException(ApiCodeConstant.RESULT_OTHER_EXCEPTION,"文件格式不支持");
        }
    }


    /**
     * 基础存储方法 返回文件存储路径
     * @param file
     * @param basePath
     * @return
     * @throws Exception
     */
    @Override
    public String store(MultipartFile file, String basePath) throws Exception {
        validateFile(file);
        return writeToStore(file.getBytes(), basePath, file.getOriginalFilename());
    }

    /**
     * 将文件按指定最大宽度压缩存储
     * @param file
     * @param basePath
     * @param maxWidth
     * @return 文件存储路径
     * @throws Exception
     */
    @Override
    public String storeScale(MultipartFile file, String basePath, int maxWidth) throws Exception {
        validateFile(file);
        byte[] bytes = ImageUtils.scaleByWidth(file, maxWidth);
        return writeToStore(bytes, basePath, file.getOriginalFilename());
    }

    /**
     * 将文件按给定的大小裁剪存储
     * @param file
     * @param basePath
     * @param width
     * @param height
     * @return 文件存储路径
     * @throws Exception
     */
    @Override
    public String storeScale(MultipartFile file, String basePath, int width, int height) throws Exception {
        validateFile(file);
        byte[] bytes = ImageUtils.screenshot(file, width, height);
        return writeToStore(bytes, basePath, file.getOriginalFilename());
    }

    /**
     *
     * 将字节数组、存储路径和原始文件名作为参数进行存储操作。
     * @param bytes
     * @param src
     * @param originalFilename
     * @return
     * @throws Exception
     */
    public String writeToStore(byte[] bytes, String src, String originalFilename) throws Exception {
        String md5 = DigestUtils.md5DigestAsHex(bytes);
        //生成文件的唯一存储路径
        String path = FilePathUtils.wholePathName(src, originalFilename);
        path = writeToStore(bytes, path);
        return path;
    }

}
